package org.gdstash.ui.select;

import org.gdstash.db.DBAffix;
import org.gdstash.db.SelectionCriteria;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.util.AdjustablePanel;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.util.List;

public class AffixPane extends AdjustablePanel {
    private static List<DBAffix> listPrefix;
    private static DBAffix[] arrPrefix;
    private static List<DBAffix> listSuffix;
    private static DBAffix[] arrSuffix;
    private JLabel lblPrefix;
    private JComboBox<DBAffix> cbPrefix;
    private JLabel lblSuffix;
    private JComboBox<DBAffix> cbSuffix;

    public static void loadAffixes() {
        if (GDStashFrame.dbConfig == null || !GDStashFrame.dbConfig.gddbInit) {
            (AffixPane.arrPrefix = new DBAffix[1])[0] = null;
            (AffixPane.arrSuffix = new DBAffix[1])[0] = null;
            return;
        }
        AffixPane.listPrefix = DBAffix.getPrefixList();
        if (AffixPane.listPrefix == null) {
            (AffixPane.arrPrefix = new DBAffix[1])[0] = null;
        } else {
            (AffixPane.arrPrefix = new DBAffix[AffixPane.listPrefix.size() + 1])[0] = null;
            int pos = 1;
            for (final DBAffix affix : AffixPane.listPrefix) {
                AffixPane.arrPrefix[pos] = affix;
                ++pos;
            }
        }
        AffixPane.listSuffix = DBAffix.getSuffixList();
        if (AffixPane.listSuffix == null) {
            (AffixPane.arrSuffix = new DBAffix[1])[0] = null;
        } else {
            (AffixPane.arrSuffix = new DBAffix[AffixPane.listSuffix.size() + 1])[0] = null;
            int pos = 1;
            for (final DBAffix affix : AffixPane.listSuffix) {
                AffixPane.arrSuffix[pos] = affix;
                ++pos;
            }
        }
    }

    static {
        AffixPane.listPrefix = null;
        AffixPane.arrPrefix = null;
        AffixPane.listSuffix = null;
        AffixPane.arrSuffix = null;
        loadAffixes();
    }

    @Override
    public void adjustUI() {
        int size = 12;
        if (GDStashFrame.iniConfig != null) {
            size = GDStashFrame.iniConfig.sectUI.fontSize;
        }
        final Dimension dimMax = new Dimension(50 * size, 2 * size);
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntCombo = UIManager.getDefaults().getFont("ComboBox.font");
        if (fntCombo == null) {
            fntCombo = fntLabel;
        }
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntCombo = fntCombo.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        this.setBorder(compound);
        if (this.lblPrefix == null) {
            this.lblPrefix = new JLabel();
        }
        this.lblPrefix.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "TXT_PREFIX"));
        this.lblPrefix.setFont(fntLabel);
        if (this.cbPrefix == null) {
            this.cbPrefix = new JComboBox<DBAffix>(AffixPane.arrPrefix);
        }
        this.cbPrefix.setFont(fntCombo);
        this.cbPrefix.setPreferredSize(dimMax);
        this.cbPrefix.setMaximumSize(dimMax);
        if (this.lblSuffix == null) {
            this.lblSuffix = new JLabel();
        }
        this.lblSuffix.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "TXT_SUFFIX"));
        this.lblSuffix.setFont(fntLabel);
        if (this.cbSuffix == null) {
            this.cbSuffix = new JComboBox<DBAffix>(AffixPane.arrSuffix);
        }
        this.cbSuffix.setFont(fntCombo);
        this.cbSuffix.setPreferredSize(dimMax);
        this.cbSuffix.setMaximumSize(dimMax);
    }

    public void updateAffixes() {
        final DefaultComboBoxModel<DBAffix> modelPrefix = new DefaultComboBoxModel<DBAffix>(AffixPane.arrPrefix);
        final DefaultComboBoxModel<DBAffix> modelSuffix = new DefaultComboBoxModel<DBAffix>(AffixPane.arrSuffix);
        this.cbPrefix.setModel(modelPrefix);
        this.cbSuffix.setModel(modelSuffix);
    }

    public void clear() {
        this.cbPrefix.getModel().setSelectedItem(null);
        this.cbSuffix.getModel().setSelectedItem(null);
    }

    public void addCriteria(final SelectionCriteria criteria) {
        int index = 0;
        criteria.prefixID = null;
        criteria.suffixID = null;
        index = this.cbPrefix.getSelectedIndex();
        if (index >= 0) {
            final DBAffix affix = (DBAffix) this.cbPrefix.getSelectedItem();
            if (affix != null) {
                criteria.prefixID = affix.getAffixID();
            }
        }
        index = this.cbSuffix.getSelectedIndex();
        if (index >= 0) {
            final DBAffix affix = (DBAffix) this.cbSuffix.getSelectedItem();
            if (affix != null) {
                criteria.suffixID = affix.getAffixID();
            }
        }
    }

    public AffixPane() {
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(false);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.lblPrefix).addComponent(this.lblSuffix)).addGroup(layout.createParallelGroup().addComponent(this.cbPrefix).addComponent(this.cbSuffix));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.lblPrefix).addComponent(this.cbPrefix)).addGroup(layout.createParallelGroup().addComponent(this.lblSuffix).addComponent(this.cbSuffix));
        layout.setVerticalGroup(vGroup);
        layout.linkSize(0, this.lblPrefix, this.lblSuffix);
        layout.linkSize(0, this.cbPrefix, this.cbSuffix);
        layout.linkSize(1, this.lblPrefix, this.cbPrefix);
        layout.linkSize(1, this.lblPrefix, this.lblSuffix);
        layout.linkSize(1, this.lblPrefix, this.cbSuffix);
    }
}
